--
-- @Author:      name
-- @DateTime:    2018-03-30 23:05:48
-- @Description: 消息的处理

local skynet = require "skynet"
local log = require "Log"
local config = require "configquery"
require "common"

local AssertEx = AssertEx


local Command = class("Command")

---------------------------------------------------------
-- Private
---------------------------------------------------------
function Command:ctor(message,machine,tableid)
	self.msg = message
	self.machine = machine
	self.tableid = tableid


	self.is_open = true
	self.close = false
	
	self:register()
		
end

--注册本服务里的消息
function Command:register()

	self.msg:registerSelf('safe_close',handler(self,self.safeClose))
	-- self.msg:registerSelf('game_switch',handler(self,self.gameSwitch))
	self.msg:registerSelf('update_prop',handler(self,self.updateProperty))
	self.msg:registerSelf('broadcast',handler(self,self.broadcast))

	self.msg:registerSelf('enter_table_req',handler(self,self.enterTable))
	self.msg:registerSelf('leave_table_req',handler(self,self.leaveTable))	
	self.msg:registerSelf('lost_connect',handler(self,self.lostConnect))
	self.msg:registerSelf('reconnect_req',handler(self,self.reconnect)) 
	self.msg:registerSelf('niuniu_status_req',handler(self,self.getStatus))
	self.msg:registerSelf('gm_set_code_req',handler(self,self.gmSet))
	self.msg:registerSelf('niuniu_bank_req',handler(self,self.bank))
	self.msg:registerSelf('niuniu_bet_req',handler(self,self.bet))
	self.msg:registerSelf('niuniu_comb_req',handler(self,self.comb))
	self.msg:registerSelf('niuniu_changecard_req',handler(self,self.changeCard))
	
end

function Command:test()
	print("###888 test###"..type(self.msg))
end
---------------------------------------------------------
-- CMD
---------------------------------------------------------

--更新属性记录
function Command:updateProperty(playerid, prop_data)
	print("_________________大厅发来玩家数据___",data)
	self.machine:updateProperty(playerid,data)	
	return true
end

--广播
function Command:broadcast( pack )
	-- self.dataCenter:send_all_player(pack)
	return true
end

--安全关服
function Command:safeClose(close)
	print("_____关牛牛")
	self.close = close
	self.machine:setClose(close)	
end



---------------------------------------------------------
-- Private
---------------------------------------------------------


---------------------------------------------------------
-- CMD
---------------------------------------------------------


---------------------------------------------------------
-- REQUEST
---------------------------------------------------------
--进入桌子
function Command:enterTable(playerid,playerInfo)
	--游戏正在维护
	AssertEx(not self.close, 'enter_table_res' , 7)
	--游戏id
	AssertEx(playerid > 0 , 'enter_table_res' , 8)
	local player = self.machine:getPlayer(playerid)
	if player then		
		if player.disconnect then --断线重连进来						
		else
			--没有断线重复请求进入桌子	
			print("_____没有断线重复请求进入桌子________")
		end
	else
		--第一次进入
		player = self.machine:addPlayer(playerInfo)
		--符合条件，自动坐下
		local bSeat = self.machine:sitDown(playerid)
		--座位已满
		AssertEx(bSeat , 'enter_table_res' , 18)
	end
	--可以开始
	--player.ready = true
	player.disconnect = false	
	local result = {}
	result.enter_table_res = {
		tableid = self.tableid,
		gameid = getGameIdbyTable(self.tableid),
	}
	return result
end

--离开
function Command:leaveTable(playerid)
	local player = self.machine:getPlayer(playerid)	
	if player then						
		if player.playing and self.machine:getState() ~= 'rest' then 
			return {leave_table_res = GetErrorTable(9)}
		end		
		self.machine:leaveTable(playerid)
	else
		--print("#############离开房间错误 未进入房间")
		return {leave_table_res = GetErrorTable(10)}
	end
	return {leave_table_res = {tableid = self.tableid}}
end

--掉线
function Command:lostConnect(playerid)
	local kickPlayer = false
	local player = self.machine:getPlayer(playerid)
	if player then
		player.disconnect = true		
		kickPlayer = self.machine:disconnect(playerid)
	else
		--log.error('>>>>>>>>>>>>>>>>>>>>############lost_connect no found playerid:',playerid)
		print('>>>>>>>>>>>>>>>>>>>>############lost_connect no found playerid:',playerid)
	end
	return kickPlayer
end

--重连
function Command:reconnect(playerid,args)
	local result = {}
	local player = self.machine:getPlayer(playerid)
	AssertEx(player , 'reconnect_res', 10)
	result = self.machine:reconnect(playerid,args)
	AssertEx(result ,'reconnect_res',15)
	player.disconnect = false
	local data = {
		reconnect_res = result
	}
	-- print("######reconnet######",data)
	return data
end

-- 得到桌子状态
function Command:getStatus( playerid )
	local player = self.machine:getPlayer(playerid)
	AssertEx(player and not player.disconnect, 'niuniu_status_res', 10)
	player.ready = true
	local status = self.machine:getTableStatus()
	return {niuniu_status_res = status}
end

--GM命令
function Command:gmSet( playerid,cards ,repeatNum)
	self.machine:setGmCard(playerid,cards,repeatNum)
end
------------------------------------------------------------------------------------------------
---游戏里的操作
------------------------------------------------------------------------------------------------

--抢庄
function Command:bank( playerid,  args)
	local player = self.machine:getPlayer(playerid)
	AssertEx(player and player.playing , 'niuniu_bank_res', 16)

	local b = self.machine:setBanker(playerid,args.rate)
	--推送玩家状态
	return {niuniu_bank_res={success=b}}	
end

--下注
function Command:bet(playerid,args)
	local player = self.machine:getPlayer(playerid)
	AssertEx(player and player.playing , 'niuniu_bet_res', 16)
	local b = self.machine:setPlayerBet(playerid,args.rate)
	--推送玩家状态
	return {niuniu_bet_res={success=b}}
end

--组牌
function Command:comb(playerid,args)
	local player = self.machine:getPlayer(playerid)	
	AssertEx(player and player.playing , 'niuniu_comb_res', 16)
	local b = self.machine:setPlayerComb(playerid)
	--推送玩家状态
	return {niuniu_comb_res={success=b}}	
end

--换牌
function Command:changeCard(playerid,args)
	local player = self.machine:getPlayer(playerid)	
	AssertEx(player and player.playing , 'niuniu_changecard_res', 16)

	local b,tbCard = self.machine:setPlayerCard(playerid)
	AssertEx(b, 'niuniu_changecard_res', 17)
	--推送玩家状态
	return {niuniu_changecard_res={card_data=tbCard}}	
end


return Command